#include "Qt3DPicker.h"

#include "Copy3DHandler.h"
#include "CmdManager.h"
#include "Scene.h"
#include "SendPointHandlerBase.h"
#include "SelectHandler3D.h"
#include "OSGNodeMask.h"
#include "Operation/OpAdd.h"
#include "PointHandler.h"
#include "QtOSGWidget.h"

Copy3DHandler::Copy3DHandler()
{
}

Copy3DHandler::~Copy3DHandler()
{
}

void Copy3DHandler::input(const QVariant& data, const QString& rawInput, CommandSource cs)
{
	auto scene = getScene();
	auto widget3D = getWidget();
	auto sph = getSendPointHandler();
	auto selector = qobject_cast<SelectHandler3D*>(getSelectHandler());
	if (data.type() == QVariant::Bool)
	{
		bool esc = !data.toBool();
		if (esc)
		{
			if (m_cloneVec.size() > 0)
			{
				widget3D->pauseRenderThread();
				for (auto clones : m_cloneVec)
				{
					for (auto clone : clones)
					{
						auto parent = clone->getParent();
						if (parent)
						{
							parent->removeChild(clone);
						}
					}
				}
				widget3D->resumeRenderThread();
			}
			selector->clearSelection();
			clearInput();
			emit finished();
			return;
		}
	}

	if (status() == 0)
	{
		m_pointStateMachine = std::make_shared<PointHandler>();
		m_pointStateMachine->setCmdManager(cmdManager());
		if (selector)
		{
			selector->clearSelection();
			selector->beginSelect(D3CurveMask | D3SurfaceMask);
			prompt(1, tr("选择对象: "));
		}
		else
		{
			emit finished();
		}
		return;
	}

	if (status() == 1)
	{
		if (selector && selector->done())
		{
			auto selections = selector->selectionSet();
			if (selections.size() > 0)
			{
				std::vector<std::shared_ptr<Ent>> clones;
				for (auto selection : selections)
				{
					std::shared_ptr<Ent> ent = std::dynamic_pointer_cast<Ent>(selection);
					auto parent = ent->getParent();
					if (parent)
					{
						UCS* ucs = dynamic_cast<UCS*>(parent);
						if (ucs)
							continue;
						auto clone = ent->clone();
						m_origins.push_back(ent);
						clones.push_back(clone);
						widget3D->pauseRenderThread();
						parent->addChild(clone);
						widget3D->resumeRenderThread();
					}
				}
				m_cloneVec.push_back(clones);
				if (m_origins.size() == 0)
				{
					remark(tr("请选择三维图形"));
					emit finished();
					return;
				}
				if (sph)
				{
					sph->setPointMask(CS_LBTNDOWN | CS_MOUSEMOVE);
				}	
				enableMouseHandler(MouseHandlerID::PointSender);
				m_pointStateMachine->setPrompt0(tr("指定第一个点"));
				setStatus(2);
				m_pointStateMachine->reset();
			}
		}
		return;
	}

	if (status() == 2)
	{
		m_pointStateMachine->input(data, rawInput, cs);
		if (m_pointStateMachine->done())
		{
			m_end = m_start = m_pointStateMachine->getPoint();
			show3DRubberLine(true);
			set3DRubberLineP1(m_start);
			set3DRubberLineP2(m_end);
			m_pointStateMachine->setPrompt0(tr("指定第二个点"));
			setStatus(3);
			m_pointStateMachine->reset();
		}
		return;
	}

	if (status() >= 3)
	{
		if (data.type() == QVariant::Bool && data.toBool())
		{
			selector->clearSelection();
			drawFinish(scene);
			emit finished();
			return;
		}
		else if (rawInput.compare("U", Qt::CaseInsensitive) == 0)
		{
			if (status() == 3)
			{
				remark(tr("无效的输入"));
				return;
			}
			assert(m_cloneVec.size() >= 2);
			widget3D->pauseRenderThread();
			for (auto clone : m_cloneVec[m_cloneVec.size() - 1])
			{
				auto parent = clone->getParent();
				if (parent)
				{
					parent->removeChild(clone);
				}
			}
			widget3D->resumeRenderThread();

			m_cloneVec.pop_back();
			if (m_cloneVec.size() > 1)
			{
				m_pointStateMachine->setPrompt0(tr("指定第二个点"), QStringList() << tr("回退"), QStringList() << "U", tr("结束"));
			}
			else
			{
				m_pointStateMachine->setPrompt0(tr("指定第二个点"));
			}
			setStatus(status() - 1);
			m_pointStateMachine->reset();
			return;
		}
		else
		{
			if (cs == CS_MOUSEMOVE)
			{
				if (data.type() == QVariant::Vector3D)
				{
					auto pt = data.value<QVector3D>();
					osg::Vec3d point = { pt.x(), pt.y(), pt.z() };
					auto thisoffset = point - m_end;
					m_end = point;
					set3DRubberLineP2(m_end);
					for (auto clone : m_cloneVec[0])
					{
						clone->move(thisoffset);
					}
				}
			}
			else
			{
				m_pointStateMachine->input(data, rawInput, cs);
				if (m_pointStateMachine->done())
				{
					auto pt = m_pointStateMachine->getPoint();
					m_pointStateMachine->reset();
					set3DRubberLineP2(pt);
					const osg::Vec3d offset = pt - m_start;
					std::vector<std::shared_ptr<Ent>> copyTemp;
					for (int i = 0; i < m_origins.size(); ++i)
					{
						auto clone = m_origins[i]->clone();
						auto parent = clone->getParent();
						if (parent)
						{
							clone->move(offset);
							widget3D->pauseRenderThread();
							parent->addChild(clone);
							widget3D->resumeRenderThread();
							copyTemp.push_back(clone);
						}	
					}
					m_cloneVec.push_back(copyTemp);
					m_pointStateMachine->setPrompt0(tr("指定第二个点"), QStringList() << tr("回退"), QStringList() << "U", tr("结束"));
					setStatus(status() + 1);
					m_pointStateMachine->reset();
				}
				return;
			}
		}
		return;
	}
}

void Copy3DHandler::drawFinish(Scene * scene)
{
	auto widget3D = getWidget();
	std::set<std::shared_ptr<INode>> result;
	widget3D->pauseRenderThread();
	for (int i = 0; i < m_cloneVec.size(); ++i)
	{
		for (auto clone : m_cloneVec[i])
		{
			auto parent = clone->getParent();
			if (parent)
			{
				parent->removeChild(clone);
			}
		}
	}
	widget3D->resumeRenderThread();

	if (m_cloneVec.size() >= 2)
	{
		auto undoStack = scene->undoStack();
		for (int i = 1; i < m_cloneVec.size(); ++i)
		{
			for (auto clone : m_cloneVec[i])
			{
				result.insert(clone);
			}
		}
		undoStack->push(new OpAdd(result));
	}
}
